package services;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import play.libs.Codec;

import com.aton.db.SessionFactory;

import models.User;
import models.mappers.UserMapper;
import models.mappers.WebHraDetailMapper;
import vos.HraDetailView;
import vos.UserInfoSearchVo;

public class UserInfoService {
	/**
	 * 根据筛选条件统计用户数量
	 *
	 * @Title: countUserInfo 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countUserInfo(UserInfoSearchVo vo) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			UserMapper mapper = ss.getMapper(UserMapper.class);
            return mapper.countUserInfo(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据筛选条件查询用户信息
	 *
	 * @Title: listUserInfo 
	 * @param vo
	 * @return
	 * @return: List<HraDetailView>
	 */
	public static List<User> listUserInfo(UserInfoSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			UserMapper mapper = ss.getMapper(UserMapper.class);
            return mapper.listUserInfo(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 重置用户密码
	 * @param operatorid 
	 *
	 * @Title: resetPass 
	 * @param idcard 身份证号
	 * @return: void
	 */
	public static boolean resetPass(User user) {
		// TODO Auto-generated method stub
		String pass=user.idcard.substring(user.idcard.length()-6,user.idcard.length());
		String password=Codec.hexMD5(pass+user.salt);
		if(password.equals(user.password)){
			return true;
		}
		user.password=password;
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try {
			UserMapper mapper = ss.getMapper(UserMapper.class);
			mapper.rePassword(user);
            ss.commit();
            return true;
        }catch(Exception e){
        	e.printStackTrace();
        	return false;
        } finally {
            ss.close();
        }
	}
	/**
	 * 更新用户信息
	 *
	 * @Title: updateUserInfo 
	 * @param user
	 * @return
	 * @return: boolean
	 */
	public static boolean updateUserInfo(User user) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try {
			UserMapper mapper = ss.getMapper(UserMapper.class);
			mapper.updateUserInfo(user);
            ss.commit();
            return true;
        }catch(Exception e){
        	e.printStackTrace();
        	return false;
        } finally {
            ss.close();
        }
	}
	/**
	 * 导出为excel
	 *
	 * @Title: exportXls 
	 * @param vo
	 * @return
	 * @return: List<User>
	 */
	public static List<User> exportXls(UserInfoSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			UserMapper mapper = ss.getMapper(UserMapper.class);
            return mapper.exportXls(vo);
        } finally {
            ss.close();
        }
	}
}
